package com.tj.collection.db.service.impl;

import com.fqgj.common.api.Page;
import com.fqgj.common.api.Response;
import com.fqgj.common.utils.CollectionUtils;
import com.fqgj.common.utils.StringUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.kly.user.chatterService.ChatterUserService;
import com.kly.user.dto.UserInfoDto;
import com.kly.user.dto.chatter.response.UserChatterModeDto;
import com.kly.user.service.UserService;
import com.tj.collection.db.dao.ChatterModelInfoExamineDao;
import com.tj.collection.db.domain.ChatterModelInfoExamine;
import com.tj.collection.db.entity.ChatterModelInfoExamineEntity;
import com.tj.collection.db.service.ProfileAuditService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ProfileAuditServiceImpl implements ProfileAuditService {

    @Resource
    ChatterModelInfoExamineDao chatterModelInfoExamineDao;
    @Resource
    ChatterUserService chatterUserService;
    @Resource
    UserService userService;

    @Override
    public List<ChatterModelInfoExamine> listProfileAudit(String chatterAccount, String modelUserCode, Integer approvalStatus, Page page) {
        // 获取查询条件chatterAccount下的所有modelUserCode
        List<String> queryByModelUserCodes = setUpQueryParams(chatterAccount);

        // 获取列表数据
        final List<ChatterModelInfoExamineEntity> chatterModelInfoExamines = chatterModelInfoExamineDao.listProfileAudit(queryByModelUserCodes, modelUserCode, approvalStatus, page);

        // chatter与model关系
        final List<String> modelUserCodes = chatterModelInfoExamines.parallelStream().map(ChatterModelInfoExamineEntity::getUserCode).collect(Collectors.toList());
        final Map<String, List<UserChatterModeDto>> groupByModelUserCode = getChatterModelMapping(modelUserCodes);

        // model数据
        final List<UserInfoDto> userInfoDtos = userService.listUserInfos(modelUserCodes);

        return chatterModelInfoExamines.stream().map(o -> {
            final ChatterModelInfoExamine vo = ChatterModelInfoExamine.builder().build();
            BeanUtils.copyProperties(o, vo);
            final UserChatterModeDto chatterDto = getChatterDto(groupByModelUserCode, o);
            vo.setChatterAccount(chatterDto.getChatterAccount());
            vo.setChatterUserCode(chatterDto.getChatterUserCode());
            vo.setChatterNickName(chatterDto.getChatterNickName());

            final UserInfoDto userInfoDto = getUserInfoDto(userInfoDtos, o);

            vo.setModelUserCode(userInfoDto.getUserCode());
            vo.setModelNickName(userInfoDto.getNickName());
            return vo;
        }).collect(Collectors.toList());


    }

    private UserInfoDto getUserInfoDto(List<UserInfoDto> userInfoDtos, ChatterModelInfoExamineEntity o) {
        return userInfoDtos.parallelStream().filter(u -> u.getUserCode().equals(o.getUserCode())).findFirst().orElse(new UserInfoDto());
    }

    private Map<String, List<UserChatterModeDto>> getChatterModelMapping(List<String> modelUserCodes) {

        // 获取chatter集合
        final List<UserChatterModeDto> modesByModelUserCodes = chatterUserService.getModesByModelUserCodes(modelUserCodes);
        if (CollectionUtils.isEmpty(modesByModelUserCodes)) {
            return Maps.newHashMap();
        }
        // 按modelUserCode分组
        return modesByModelUserCodes.parallelStream().collect(Collectors.groupingBy(UserChatterModeDto::getModelUserCode));
    }

    private List<String> setUpQueryParams(String chatterAccount) {
        List<String> queryByModelUserCodes = Lists.newArrayList();
        if (StringUtils.isNotEmpty(chatterAccount)) {
            final List<UserChatterModeDto> modesByModelUserCodes = chatterUserService.getModesByChatterAccount(chatterAccount);
            if (CollectionUtils.isNotEmpty(modesByModelUserCodes)) {
                queryByModelUserCodes = modesByModelUserCodes.parallelStream().map(UserChatterModeDto::getModelUserCode).collect(Collectors.toList());
            }
        }
        return queryByModelUserCodes;
    }

    private UserChatterModeDto getChatterDto(Map<String, List<UserChatterModeDto>> groupByModelUserCode, ChatterModelInfoExamineEntity o) {
        final List<UserChatterModeDto> userChatterModeDtos = groupByModelUserCode.get(o.getUserCode());
        if (CollectionUtils.isEmpty(userChatterModeDtos)) {
            return new UserChatterModeDto();
        }
        return userChatterModeDtos.parallelStream().filter(chatter -> o.getUserCode().equals(chatter.getModelUserCode())).findFirst().orElse(new UserChatterModeDto());
    }

    @Override
    public int getProfileAuditCnt(String chatterAccount, String modelUserCode, Integer approvalStatus) {

        return chatterModelInfoExamineDao.getProfileAuditCnt(setUpQueryParams(chatterAccount), modelUserCode, approvalStatus);
    }

    @Override
    public Response<ChatterModelInfoExamine> approval(Integer id, Integer approvalStatus, String dealUserCode) {
        if (id == null) {
            return Response.error("The id should not be empty");
        }
        if (approvalStatus == null) {
            return Response.error("The approvalStatus should not be empty");
        }

        final ChatterModelInfoExamineEntity entity = chatterModelInfoExamineDao.getById(id);
        if (entity == null) {
            return Response.error("Do not found in record");
        }

        entity.setOperateStatus(approvalStatus);
        entity.setDealUserCode(dealUserCode);
        entity.setDealTime(new Date());
        final int approval = chatterModelInfoExamineDao.approval(id, approvalStatus, dealUserCode);
        if (approval != 1) {
            return Response.error("Approval Failed");
        }

        final ChatterModelInfoExamine vo = ChatterModelInfoExamine.builder().build();
        BeanUtils.copyProperties(entity, vo);
        vo.setModelUserCode(entity.getUserCode());
        return Response.ok(vo);
    }
}
